<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  <title>Cordova+Vue实现点击两次返回退出应用 | Storm</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="Cordova + Vue 实现点击两次退出应用注册事件123456789101112131415//注意在 deviceready 后使用,写在methods中//点击返回按键  onBackKeyDown() &amp;#123;       this.$toast(&apos;再点击一次退出应用&apos;);       document.removeEventListener(&quot;backbutton&quot;, this.">
<meta name="keywords" content="Cordova Vue">
<meta property="og:type" content="article">
<meta property="og:title" content="Cordova+Vue实现点击两次返回退出应用">
<meta property="og:url" content="https://storm4542.github.io/archives/81b0b94a.html">
<meta property="og:site_name" content="Storm">
<meta property="og:description" content="Cordova + Vue 实现点击两次退出应用注册事件123456789101112131415//注意在 deviceready 后使用,写在methods中//点击返回按键  onBackKeyDown() &amp;#123;       this.$toast(&apos;再点击一次退出应用&apos;);       document.removeEventListener(&quot;backbutton&quot;, this.">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2019-03-12T06:14:53.854Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Cordova+Vue实现点击两次返回退出应用">
<meta name="twitter:description" content="Cordova + Vue 实现点击两次退出应用注册事件123456789101112131415//注意在 deviceready 后使用,写在methods中//点击返回按键  onBackKeyDown() &amp;#123;       this.$toast(&apos;再点击一次退出应用&apos;);       document.removeEventListener(&quot;backbutton&quot;, this.">
  
    <link rel="alternative" href="https://storm4542.github.io/atom.xml" title="Storm" type="application/atom+xml">
  
  
    <link rel="icon" href="https://storm4542.github.io/favicon.png">
  
  <link href="static/css/css.css" rel="stylesheet" type="text/css">
  <link rel="stylesheet" href="static/css/style.css">
  

</head></html>
<body>
  <div id="container">
    <div id="wrap">
      <div class="outer">
        <section id="main"><article id="post-Cordova-Vue实现点击两次返回退出应用" class="article article-type-post" itemscope="" itemprop="blogPost">
  
  <div class="article-inner">
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      Cordova+Vue实现点击两次返回退出应用
    </h1>
  

      </header>
    
    <div class="article-meta">
      <a href="" class="article-date">
  <time datetime="2019-03-12T06:08:54.000Z" itemprop="datePublished">2019-03-12</time>
</a>
      
    </div>
    <div class="article-entry" itemprop="articleBody">
      
        <h3 id="Cordova-Vue-实现点击两次退出应用"><a href="#Cordova-Vue-实现点击两次退出应用" class="headerlink" title="Cordova + Vue 实现点击两次退出应用"></a>Cordova + Vue 实现点击两次退出应用</h3><h4 id="注册事件"><a href="#注册事件" class="headerlink" title="注册事件"></a>注册事件</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//注意在 deviceready 后使用,写在methods中</span></span><br><span class="line"><span class="comment">//点击返回按键</span></span><br><span class="line">  onBackKeyDown() &#123;</span><br><span class="line">       <span class="keyword">this</span>.$toast(<span class="string">'再点击一次退出应用'</span>);</span><br><span class="line">       <span class="built_in">document</span>.removeEventListener(<span class="string">"backbutton"</span>, <span class="keyword">this</span>.onBackKeyDown, <span class="literal">false</span>); <span class="comment">// 注销返回键</span></span><br><span class="line">       <span class="built_in">document</span>.addEventListener(<span class="string">"backbutton"</span>, <span class="keyword">this</span>.exitApp, <span class="literal">false</span>);<span class="comment">//绑定退出事件</span></span><br><span class="line">       setInterval(<span class="function"><span class="params">()</span> =&gt;</span> &#123;</span><br><span class="line">             <span class="built_in">document</span>.addEventListener(<span class="string">"backbutton"</span>, <span class="keyword">this</span>.onBackKeyDown, <span class="literal">false</span>);</span><br><span class="line">             <span class="built_in">document</span>.removeEventListener(<span class="string">"backbutton"</span>, <span class="keyword">this</span>.exitApp, <span class="literal">false</span>);</span><br><span class="line">           &#125;, <span class="number">3000</span>)</span><br><span class="line">        &#125;</span><br><span class="line"><span class="comment">//关闭APP</span></span><br><span class="line"> exitApp() &#123;</span><br><span class="line">         navigator.app.exitApp();</span><br><span class="line">        &#125;,</span><br></pre></td></tr></table></figure>
<h4 id="启动事件"><a href="#启动事件" class="headerlink" title="启动事件"></a>启动事件</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">created() &#123;</span><br><span class="line">      <span class="built_in">document</span>.addEventListener(<span class="string">"backbutton"</span>, <span class="keyword">this</span>.onBackKeyDown, <span class="literal">false</span>);</span><br><span class="line">      <span class="keyword">this</span>.refreshTask();</span><br><span class="line">      <span class="keyword">this</span>.refreshNotice();</span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure>
<h4 id="销毁事件"><a href="#销毁事件" class="headerlink" title="销毁事件"></a>销毁事件</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">beforeDestroy() &#123;</span><br><span class="line">       <span class="built_in">document</span>.removeEventListener(<span class="string">"backbutton"</span>, <span class="keyword">this</span>.onBackKeyDown, <span class="literal">false</span>); <span class="comment">// 注销返回键</span></span><br><span class="line">       <span class="built_in">document</span>.removeEventListener(<span class="string">"backbutton"</span>, <span class="keyword">this</span>.exitApp, <span class="literal">false</span>);</span><br><span class="line">   &#125;</span><br></pre></td></tr></table></figure>
<p>如果页面使用了<code>&lt;keep-alive&gt;</code>标签，那么销毁事件的时机为页面离开之前。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">beforeRouteLeave(to, <span class="keyword">from</span>, next) &#123;</span><br><span class="line">    <span class="built_in">document</span>.removeEventListener(<span class="string">"backbutton"</span>, <span class="keyword">this</span>.onBackKeyDown, <span class="literal">false</span>); <span class="comment">// 注销返回键</span></span><br><span class="line">    <span class="built_in">document</span>.removeEventListener(<span class="string">"backbutton"</span>, <span class="keyword">this</span>.exitApp, <span class="literal">false</span>);</span><br><span class="line">    <span class="keyword">this</span>.$indicator.close()</span><br><span class="line">    next()</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

      

      
        
    </div>
  </div>
  
    
<nav id="article-nav">
  
    <a href="8ea8a281.html" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          使用 Vue Cli 3 打包组件并发布至npm
        
      </div>
    </a>
  
  
    <a href="990bab20.html" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">Cordova下载并预览功能</div>
    </a>
  
</nav>

  
</article>


<section id="comments">
  <link rel="stylesheet" href="static/css/default.css">
  <script src="static/js/gitment.browser.js"></script>
  <script>
    var gitment = new Gitment({
      owner: 'your github account',
      repo: 'your repo',
      oauth: {
        client_id: 'your client_id',
        client_secret: 'your client_secret',
      },
    })
    gitment.render('comments')
  </script>
</section>

</section>
        <aside id="sidebar">
  <nav class="menus">
  	<ul>
  		<li><a href="index.html"><i class="icon icon-home"></i></a></li>
  		
			<li><a href="index1.html"><i class="icon icon-fenlei"></i></a></li>
  		
  		
			<li><a href="index2.html"><i class="icon icon-tag"></i></a></li>
  		
  		
  			<li><a href="javascript:;" target="_blank" rel="external nofollow noopener noreferrer"><i class="icon icon-github"></i></a></li>
  		
  	</ul>
  </nav>
  <a id="go-top" href="#"><i class="icon icon-up"></i></a>
</aside>

      </div>
      <footer id="footer">
  
	<div id="footer-info" class="inner">
	  &copy; 2019 Storm 
	  - Powered by <a href="javascript:;" target="_blank" rel="external nofollow noopener noreferrer">Hexo</a>
	  - Theme <a href="javascript:;" target="_blank" rel="external nofollow noopener noreferrer">Jane</a>
	</div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="index.html" class="mobile-nav-link">Home</a>
  
    <a href="index1.html" class="mobile-nav-link">Archives</a>
  
    <a href="index2.html" class="mobile-nav-link">Tag</a>
  
    <a href="javascript:;" class="mobile-nav-link" rel="external nofollow noopener noreferrer" target="_blank">Github</a>
  
</nav>
    
<script>
  var disqus_shortname = 'storm';
  
  var disqus_url = 'https://storm4542.github.io/archives/81b0b94a.html';
  
  (function(){
    var dsq = document.createElement('script');
    dsq.type = 'text/javascript';
    dsq.async = true;
    dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
    (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
  })();
</script>


<script src="static/js/jquery.min.js"></script>


  <link rel="stylesheet" href="static/css/jquery.fancybox.css">
  <script src="static/js/jquery.fancybox.pack.js"></script>


<script src="static/js/script.js"></script>

  </div>
</body>
